/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.Color;
import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author snake_gt
 */
public class ProductoCrearEditar extends javax.swing.JFrame {

    /**
     * Creates new form ProductoCrearEditar
     */
    private ProductoLnLocal productoControl;
    private TipoVentana tipoVentana;
    private Producto producto;
    public ProductoCrearEditar(Producto producto,ProductoLnLocal productoControl, TipoVentana tipoVentana) {
        this.productoControl = productoControl;
        this.tipoVentana = tipoVentana;
        this.producto=producto;
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                jLabel6.setText("NUEVO PRODUCTO");
                break;
            case VENTANA_MODIFICAR:
                jLabel6.setText("MODIFICAR PRODUCTO");
                jTextField1.setEditable(false);
                jTextField1.setText(producto.getCodigo()+"");
                jTextField2.setText(producto.getNombre());
                jTextField3.setText(producto.getPrecio()+"");
                jTextField4.setText(producto.getDepartamento());
                jTextField5.setText(producto.getDescripcion());
                break;
            case VENTANA_BUSQUEDA:
                jLabel6.setText("BUSCAR POR ID");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                BotonCancelar.setVisible(false);
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        BotonAceptar = new javax.swing.JButton();
        BotonCancelar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridLayout(5, 2));

        jLabel1.setText("Codigo");
        jPanel1.add(jLabel1);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        jPanel1.add(jTextField1);

        jLabel2.setText("Nombre");
        jPanel1.add(jLabel2);
        jPanel1.add(jTextField2);

        jLabel3.setText("Precio");
        jPanel1.add(jLabel3);
        jPanel1.add(jTextField3);

        jLabel4.setText("Departamento");
        jPanel1.add(jLabel4);
        jPanel1.add(jTextField4);

        jLabel5.setText("Descripcion");
        jPanel1.add(jLabel5);
        jPanel1.add(jTextField5);

        jPanel2.add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridLayout(1, 2));

        BotonAceptar.setText("Aceptar");
        BotonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAceptarActionPerformed(evt);
            }
        });
        jPanel3.add(BotonAceptar);

        BotonCancelar.setText("Cancelar");
        BotonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCancelarActionPerformed(evt);
            }
        });
        jPanel3.add(BotonCancelar);

        jPanel2.add(jPanel3, java.awt.BorderLayout.PAGE_END);

        jLabel6.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 51));
        jLabel6.setText("TITULO");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jLabel6)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel6)
                .addGap(38, 38, 38)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(75, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_BotonCancelarActionPerformed

    private void BotonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAceptarActionPerformed
        // TODO add your handling code here:
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                Producto p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.registrar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_BUSQUEDA:
                this.dispose();
                break;
            case VENTANA_MODIFICAR:
                p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.modificar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                break;
                
        }


    }//GEN-LAST:event_BotonAceptarActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
        if(tipoVentana!=TipoVentana.VENTANA_REGISTRO){
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                Producto p=productoControl.buscarPorCodigo(Integer.parseInt(jTextField1.getText()));
                if(p!=null){
                    jTextField2.setText(p.getNombre());
                    jTextField3.setText(p.getPrecio()+"");
                    jTextField4.setText(p.getDepartamento());
                    jTextField5.setText(p.getDescripcion());
                    if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                        jTextField2.setEditable(true);
                        jTextField3.setEditable(true);
                        jTextField4.setEditable(true);
                        jTextField5.setEditable(true);
                    }
                }
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAceptar;
    private javax.swing.JButton BotonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
